package com.supermarket.controller;

import com.supermarket.common.ResponseResult;
import com.supermarket.entity.Favorites;
import com.supermarket.entity.User;
import com.supermarket.service.FavoritesService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.HashMap;

@RestController
@RequestMapping("/favorites")
public class FavoritesController {
    @Resource
    private FavoritesService favoritesService;
    
    /**
     * 获取用户收藏列表
     * 
     * @param request HTTP请求，用于获取当前登录用户
     * @return 收藏列表
     */
    @GetMapping("/list")
    public ResponseResult<List<Favorites>> getUserFavorites(HttpServletRequest request) {
        // 从session中获取用户信息
        HttpSession session = request.getSession();
        User currentUser = (User) session.getAttribute("currentUser");
        
        if (currentUser == null) {
            return ResponseResult.error(401, "请先登录");
        }
        
        Integer userId = currentUser.getId();
        List<Favorites> favorites = favoritesService.getUserFavorites(userId);
        return ResponseResult.success(favorites);
    }
    
    /**
     * 获取指定用户的收藏列表（管理员使用）
     *
     * @param userId 用户ID
     * @return 收藏列表
     */
    @GetMapping("/list/user")
    public ResponseResult<List<Favorites>> getUserFavoritesById(@RequestParam Integer userId) {
        List<Favorites> favorites = favoritesService.getUserFavorites(userId);
        return ResponseResult.success(favorites);
    }
    
    /**
     * 添加收藏
     *
     * @param params 包含commodityId的参数
     * @param request HTTP请求，用于获取当前登录用户
     * @return 操作结果
     */
    @PostMapping("/add")
    public ResponseResult<String> addFavorites(@RequestBody Map<String, Integer> params, HttpServletRequest request) {
        // 从session中获取用户信息
        HttpSession session = request.getSession();
        User currentUser = (User) session.getAttribute("currentUser");
        
        if (currentUser == null) {
            return ResponseResult.error(401, "请先登录");
        }
        
        Integer userId = currentUser.getId();
        Integer commodityId = params.get("commodityId");
        
        if (commodityId == null) {
            return ResponseResult.error(400, "商品ID不能为空");
        }
        
        int result = favoritesService.addFavorites(userId, commodityId);
        if (result > 0) {
            return ResponseResult.success("收藏成功");
        } else {
            return ResponseResult.error(500, "收藏失败");
        }
    }
    
    /**
     * 取消收藏
     *
     * @param commodityId 商品ID
     * @param request HTTP请求，用于获取当前登录用户
     * @return 操作结果
     */
    @DeleteMapping("/remove")
    public ResponseResult<String> removeFavorites(
            @RequestParam Integer commodityId,
            HttpServletRequest request) {
        // 从session中获取用户信息
        HttpSession session = request.getSession();
        User currentUser = (User) session.getAttribute("currentUser");
        
        if (currentUser == null) {
            return ResponseResult.error(401, "请先登录");
        }
        
        Integer userId = currentUser.getId();
        
        int result = favoritesService.removeFavorites(userId, commodityId);
        if (result > 0) {
            return ResponseResult.success("取消收藏成功");
        } else {
            return ResponseResult.error(400, "取消收藏失败，可能未收藏该商品");
        }
    }
    
    /**
     * 删除收藏
     *
     * @param id 收藏ID
     * @return 操作结果
     */
    @DeleteMapping("/delete")
    public ResponseResult<String> deleteFavorites(@RequestParam Integer id) {
        int result = favoritesService.deleteFavorites(id);
        if (result > 0) {
            return ResponseResult.success("删除收藏成功");
        } else {
            return ResponseResult.error(400, "删除收藏失败");
        }
    }
    
    /**
     * 清空用户收藏夹
     *
     * @param request HTTP请求，用于获取当前登录用户
     * @return 操作结果
     */
    @DeleteMapping("/clear")
    public ResponseResult<String> clearFavorites(HttpServletRequest request) {
        // 从session中获取用户信息
        HttpSession session = request.getSession();
        User currentUser = (User) session.getAttribute("currentUser");
        
        if (currentUser == null) {
            return ResponseResult.error(401, "请先登录");
        }
        
        Integer userId = currentUser.getId();
        
        int result = favoritesService.clearFavorites(userId);
        return ResponseResult.success("清空收藏夹成功，共删除" + result + "条收藏");
    }
    
    /**
     * 检查用户是否已收藏某商品
     *
     * @param commodityId 商品ID
     * @param request HTTP请求，用于获取当前登录用户
     * @return 是否已收藏
     */
    @GetMapping("/check")
    public ResponseResult<Map<String, Object>> checkFavorite(
            @RequestParam Integer commodityId,
            HttpServletRequest request) {
        // 从session中获取用户信息
        HttpSession session = request.getSession();
        User currentUser = (User) session.getAttribute("currentUser");
        
        if (currentUser == null) {
            return ResponseResult.error(401, "请先登录");
        }
        
        Integer userId = currentUser.getId();
        
        boolean isFavorite = favoritesService.isFavorite(userId, commodityId);
        Map<String, Object> result = new HashMap<>();
        result.put("isFavorite", isFavorite);
        return ResponseResult.success(result);
    }

}
